<?php

namespace Yolo\Tests;

use Hyperf\Testing\Http\TestResponse;
use Hyperf\Testing\TestCase;
use function Hyperf\Support\env;

/**
 * AbstractTest
 * @author lushaoming<lushaoming6@gmail.com>
 * @date 2024/1/26
 */
abstract class AbstractTest extends TestCase
{
    /**
     * @var string $service 服务名称
     */
    protected string $service;

    /**
     * @var RpcClient $rpcClient rpc客户端
     */
    protected RpcClient $rpcClient;

    public function __construct(string $name)
    {
        parent::__construct($name);

        $this->rpcClient = new RpcClient($this->service);
    }

    public function invoke(string $api, array $args, array $metadata = []): TestResponse
    {
        $response = $this->rpcClient->invokeByNs($this->service, 'Api', $api, $args, $metadata);

        return $this->createTestResponse($response);
    }

    public function invokeByNs(string $ns, string $api, array $args, array $metadata = []): TestResponse
    {
        $response = $this->rpcClient->invokeByNs($this->service, $ns, $api, $args, $metadata);

        return $this->createTestResponse($response);
    }

    protected function createTestResponse($response): TestResponse
    {
        return new TestResponse($response);
    }
}